package com.jyu.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jyu.reggie.common.R;
import com.jyu.reggie.entity.Orders;
import com.jyu.reggie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/submit")
    public R<String> save(@RequestBody Orders orders){
        orderService.submit(orders);

        return R.success("下单成功");
    }

    /**
     *分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number,String beginTime,String endTime){
        Page pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(number!=null,Orders::getNumber,number);

        queryWrapper.between(beginTime != null && endTime != null,Orders::getOrderTime,beginTime,endTime);

        queryWrapper.orderByDesc(Orders::getOrderTime);

        orderService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }


    @PutMapping
    public R<String> change(@RequestBody  Orders orders){
        Long id = orders.getId();
        Integer status = orders.getStatus();

        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();

        updateWrapper.eq(id!= null,Orders::getId,id);

        updateWrapper.set(Orders::getStatus,status);

        orderService.update(updateWrapper);

        return R.success("状态已成功修改");
    }

}
